﻿using Syspetro.Core.AppDeal;
using Syspetro.Core.CommandBus;
using Syspetro.Core.Resources;
using Syspetro.Core.SysInterface;
using Syspetro.Deploy.Managers;
using Syspetro.DepRedis;

namespace BJ.AuthDomain.Commands
{
    /// <summary>
    /// 手机验证校验
    /// </summary>
    public class SMSVerifyCommand : Command
    {
        public string Captcha { get; set; }
        public string Phone { get; set; }
    }
    public interface ISMSVerifyCommandHandler : ICommandHandler<SMSVerifyCommand>, ITransientService
    {

    }
    public class SMSVerifyCommandHandler : CommandHandler<SMSVerifyCommand>, ISMSVerifyCommandHandler
    {
        readonly IAuthorizationManager _authorization;
        public SMSVerifyCommandHandler(IAuthorizationManager authorization)
        {
            _authorization = authorization;
        }


        public override SpActionResult<string> Execute(SMSVerifyCommand command)
        {

            //if (command.Captcha == "111111")
            //    return new SpActionResult<string>("");

            if (string.IsNullOrEmpty(command.Phone))
                command.Phone = _authorization.GetPhone;
            if (string.IsNullOrEmpty(command.Phone))
                return new SpActionResult<string> { Success = false, Msg = "手机号为空" };
            RedisHelper redis = new("SMSCode");
            var _code = redis.StringGet(command.Phone);
            if (string.IsNullOrEmpty(command.Captcha))
            {
                return new SpActionResult<string> { Success = false, Msg = SharedResource.VerifyCannotEmpty  };
            }
            else if (_code == command.Captcha)
            {
                return new SpActionResult<string> { Success = true, Msg = SharedResource.VerifyCodeOk };
            }
            else
            {

                return new SpActionResult<string> { Success = false, Msg = SharedResource.VerifyCodeError };
            }
        }
    }
}
